/**
 * <p>Title: SignatureRest</p>
 * <p>Description: The Impl of Signature Rest</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Huang Zhongwei
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-28  Huang Zhongwei  Create
 *</pre>
*/
package cn.jhg.account.rest;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;

import org.bson.Document;
import org.springframework.stereotype.Component;

import cn.jhg.account.entity.Authority;
import cn.jhg.account.entity.SignatureDefinition;
import cn.jhg.account.entity.UserGroup;
import cn.jhg.account.service.AuthoritySvc;
import cn.jhg.account.service.SignatureSvc;
import cn.jhg.account.service.UserGroupSvc;
import cn.jhg.account.util.JsonUtil;
import cn.jhg.account.util.ServiceResult;

import net.sf.json.JSONObject;

/**
 * 签名资源
 * @author Huang Zhongwei
 *
 */
@Component
public class SignatureRestImpl implements SignatureRest{

	@Resource
	private SignatureSvc signatureSvc;
	@Resource 
	private UserGroupSvc userGroupSvc;
	@Resource
	private AuthoritySvc authoritySvc;
	
	/**
	 * 查询签名定义
	 * @param id	签名定义id
	 * @return		
	 */
	@Override
	public String querySignatureDefinition(int id) {
		// TODO Auto-generated method stub
		ServiceResult<Authority> auth=authoritySvc.getAuthorityById(id, 1);
		if(auth.getErrorCode()!=0){
			return JsonUtil.packaging(auth);
		}
		
		int sid=auth.getData().getSid();
		System.out.println(sid);
		if(sid==0){
			int type=-1;
			JSONObject jobj=new JSONObject();
			jobj.put("errorCode",0);
			jobj.put("type",type);
			return jobj.toString();
		}
		
		ServiceResult<SignatureDefinition> Serviceresult=signatureSvc.querySignatureDefinition(sid, 0);
		if(Serviceresult.getErrorCode()!=0){
			return JsonUtil.packaging(Serviceresult);
		}
		
		SignatureDefinition sign=Serviceresult.getData();
		System.out.println(sign);
		int option=sign.getOption();
		int type=(option & 4)/4;
		int signself=(option & 2)/2;
		int remark=option & 1;
		int authid=Serviceresult.getData().getAuthid();
		int authid2=Serviceresult.getData().getAuthid2();
		
		List<UserGroup> list1=authoritySvc.findUserGroupsByAid(authid).getData();
		List<UserGroup> list2=new ArrayList<UserGroup>();
//		List<User> list2=new ArrayList<User>();
//		List<User> list1=new ArrayList<User>();
		if(type==1){
			list2=authoritySvc.findUserGroupsByAid(authid2).getData();
//			ServiceResult<List<Integer>> ugids2=authoritySvc.findUserGroupByAid(authid2);
//			//创建集合添加用户
//			for(Integer ugid:ugids2.getData()){
//				ServiceResult<List<User>> list=userGroupSvc.findUsers(ugid, 1);
//				list2.addAll(list.getData());
//			}
//			list2=ListUtil.removeDuplicateWithOrder(list2);
		}
//		ServiceResult<List<Integer>> ugids=authoritySvc.findUserGroupByAid(authid);
//		//创建集合添加用户
//		
//		for(Integer ugid:ugids.getData()){
//			ServiceResult<List<User>> list=userGroupSvc.findUsers(ugid, 1);
//			list1.addAll(list.getData());
//		}
//		list1=ListUtil.removeDuplicateWithOrder(list1);
		
		JSONObject jobj=new JSONObject();
		jobj.put("errorCode",0);
		jobj.put("sign",JSONObject.fromObject(sign));
		jobj.put("user1",list1);
		jobj.put("user2",list2);
		jobj.put("type",type);
		jobj.put("signself",signself);
		jobj.put("remark",remark);
		return jobj.toString();
	}
	
	/**
	 * 签名确认
	 * @param sid  签名id
	 * @param comment 签名备注
	 * @return
	 */
	@Override
	public String signConfirmation(int sid, String account, String password, String comment) {
		// TODO Auto-generated method stub
		Document metadata=new Document();
		metadata.append("sdid",sid);
		metadata.append("comment",comment);
		ServiceResult<Integer> result=signatureSvc.signConfirmation(sid, account, password, metadata, 0);
		return JsonUtil.packaging(result);
	}
	
	/**
	 * 确认签名
	 * @param id	签名id
	 * @return		
	 */
	@Override
	public String signConfirmations(int sdid,UriInfo ui) {
		// TODO Auto-generated method stub
		ServiceResult<Integer> result=new ServiceResult<Integer>();
		MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
		String account=queryParams.get("user1[account]").get(0);
		String password=queryParams.get("user1[password]").get(0);
		String comment=queryParams.get("user1[comment]").get(0);
		
		if(!queryParams.containsKey("user2[account]")){
			Document metadata=new Document();
			metadata.append("sdid",sdid);
			metadata.append("comment",comment);
			result=signatureSvc.signConfirmation(sdid, account, password, metadata, 0);
			
		}else{
			String account2=queryParams.get("user2[account]").get(0);
			String password2=queryParams.get("user2[password]").get(0);
			String comment2=queryParams.get("user2[comment]").get(0);
			
			Document metadata=new Document();
			metadata.append("sdid",sdid);
			metadata.append("account1",account);
			metadata.append("comment",comment);
			metadata.append("account2",account);
			metadata.append("comment2",comment2);
			result=signatureSvc.signConfirmation(sdid, account, password, account2, password2, metadata, 1);
		}
		return JsonUtil.packaging(result);
		
		
//		System.out.println(queryParams.get("account"));
//		System.out.println(queryParams.size());
//		System.out.println(queryParams.getFirst("password"));
//		System.out.println(queryParams.getFirst("comment"));
	}

}
